var _qIndex = 0,_questions,_lastIndex,_singleIndex=0,
	_evaluationResult={
		sex:"",
		age:"",
		result:[]
	};
var wvMould = new(function(){
	var _that = this;
	this.getData = function(){
		$.getJSON("data/pos-psy.json",function(data){
			_questions = data.questions;
			_lastIndex = data.questions.length;
			_that.init();
		});
	};
	this.init = function(_index){
		//alert(5);

		_index = _index || 0;
		var _item = _questions[_index];
		var _prefix = (_index+1)+"/"+_questions.length+".";
		switch(true){
			case _item.type == "imagesChoice":
				_that._style1(_item,_prefix);
				break;
			case _item.type == "trueOrFalse":
				_that._style2(_item,_prefix);
				break;
			case _item.type == "multipleChoice":
				_that._style4(_item,_prefix);
				break;
			case _item.type == "singleChoice":
				_that._style3(_item,_prefix);
				break;
			case _item.type == "singleChoiceGroup":
			//alert(6);
				_that._style5(_item,_index,_prefix);
				break;
			default :
				break;
		}

	}
	this._style1 = function(item,_prefix){
		var _imgIndex = 0;
		var page = $('<div class="wv-page">'+
					'	<p class="wv-title">'+_prefix+item.title+'</p>'+
					'	<p class="wv-title-sm wv-font-tilt">'+item.content+'</p>'+
					'	<div class="wv-bar-box" data_qId="'+item.id+'"></div>'+
					'</div>');
		for(var i = 0; i < item.options.length; i++){
			if(i==0){
				var _li = $('<img class="wv-img-start" src="'+item.options[i].value+'" />');
				page.find(".wv-bar-box").append(_li);
			}else{
				var _li = $('<img class="wv-img" data_aId="'+item.options[i].id+'" src="'+item.options[i].value+'" />');
				page.find(".wv-bar-box").append(_li);
			}
		}

		page.find(".wv-img-start").on("click",function(){
			$(this).parents(".wv-bar-box").find(".wv-img").removeClass("active");
			$(this).parents(".wv-bar-box").find(".wv-img:eq("+_imgIndex+")").addClass("active");
			_imgIndex += 1;
			$(this).hide().remove();
			_that._checkVal(page,"wv-img",1);
		});
		page.find(".wv-img").on("click",function(){
			$(this).parents(".wv-bar-box").find(".wv-img").removeClass("active");
			$(this).parents(".wv-bar-box").find(".wv-img:eq("+_imgIndex+")").addClass("active");
			_imgIndex += 1;
			var _maxIndex = $(this).parents(".wv-bar-box").find(".wv-img").length;
			if(_imgIndex > _maxIndex-1){
				_imgIndex = 0;
			}
			_that._checkVal(page,"wv-img",1);
		});

		_that.btnEvent(function(){
			var _qResult = {
				id:$(".wv-page .wv-bar-box").attr("data_qId"),
				rst:[$(".wv-page .wv-bar-box .wv-img.active").attr("data_aId")]
			}
			_evaluationResult.result.push(_qResult);
		});
		$(".wv-content").empty().append(page);
	};
	this._style2 = function(item,_prefix){
		var page = $('<div class="wv-page">'+
					'	<p class="wv-title">'+_prefix+item.title+'</p>'+
					'	<div class="wv-bar-box">'+
					'		<ul class="wv-list"></ul>'+
					'	</div>'+
					'</div>');
		for(var i = 0; i < item.subQuestions.length; i++){
			var _li = $('<li class="wv-radio-bar" data_qId="'+item.subQuestions[i].id+'">'+
						'	<span class="wv-radio-box wv-radio-left" data_aId="'+item.options[1].id+'">'+
						'		<input type="radio" class="wv-radio" name="radio" />'+
						'	</span>'+item.subQuestions[i].content+
						'	<span class="wv-radio-box wv-radio-right" data_aId="'+item.options[0].id+'">'+
						'		<input type="radio" class="wv-radio" name="radio" />'+
						'	</span>'+
						'</li>')
			page.find(".wv-list").append(_li);
		}
		page.find(".wv-radio-box").on("click",function(){
			$(this).siblings(".wv-radio-box").removeClass("active");
			$(this).addClass("active");
			_that._checkVal(page,"wv-radio-box",$(this).parents(".wv-list").find(".wv-radio-bar").length);
		});

		_that.btnEvent(function(){
			$.each($(".wv-page .wv-radio-bar"),function(i,item) {
				var _qResult = {
					id:$(item).attr("data_qId"),
					rst:[$(item).find(".wv-radio-box.active").attr("data_aId")]
				}
				_evaluationResult.result.push(_qResult);
			});
		});
		$(".wv-content").empty().append(page);
	};
	this._style3 = function(item,_prefix){
		var page = $('<div class="wv-page">'+
					'	<p class="wv-title">'+_prefix+item.title+'</p>'+
					'	<p class="wv-title-sm wv-font-tilt"></p>'+
					'	<div class="wv-bar-box  wv-txt-center" data_qId="'+item.id+'" ></div>'+
					'</div>');
		if(item.options.length == 4){
			page.find(".wv-bar-box").addClass("wv-bar-smblock");
		}else{
			page.find(".wv-bar-box").addClass("wv-bar-lgblock");
		}
		for(var i = 0; i < item.options.length; i++){
			var _li = $('<div class="wv-item">'+
						'	<span class="wv-radio-lg" data_aId="'+item.options[i].id+'">'+item.options[i].value+
						'		<input type="radio" class="wv-radio" name="radio" />'+
						'	</span>'+
						'</div>');
			page.find(".wv-bar-box").append(_li);
		}
		page.find(".wv-radio-lg").on("click",function(){
			$(this).parents(".wv-bar-box").find(".wv-radio-lg").removeClass("active");
			$(this).addClass("active");
			_that._checkVal(page,"wv-radio-lg",1);
		});

		_that.btnEvent(function(){
			var _qResult = {
				id:$(".wv-page .wv-bar-box").attr("data_qId"),
				rst:[$(".wv-page .wv-bar-box .wv-radio-lg.active").attr("data_aId")]
			}
			_evaluationResult.result.push(_qResult);
		});
		$(".wv-content").empty().append(page);
	};
	this._style4 = function(item,_prefix){
		var page = $('<div class="wv-page">'+
					'	<p class="wv-title">'+_prefix+item.title+'</p>'+
					'	<p class="wv-title-sm wv-font-tilt"></p>'+
					'	<div class="wv-bar-box wv-bar-round" data_qId="'+item.id+'"></div>'+
					'</div>');
		for(var i = 0; i < item.options.length; i++){
			var _li = $('<div class="wv-item">'+
						'	<span class="wv-radio-circle" data_aId="'+item.options[i].id+'">'+item.options[i].value+
						'		<input type="radio" class="wv-radio" name="radio" />'+
						'	</span>'+
						'</div>');
			page.find(".wv-bar-box").append(_li);
		}
		page.find(".wv-radio-circle").on("click",function(){
			if($(this).hasClass("active")){
				$(this).removeClass("active");
			}else{
				if($(this).parents(".wv-bar-box").find(".wv-radio-circle.active").length < item.max){
					$(this).addClass("active");
				}
			}
			_that._checkVal(page,"wv-radio-circle",item.min);
		});

		_that.btnEvent(function(){
			var _qResult = {
				id:$(".wv-page .wv-bar-box").attr("data_qId"),
				rst:[]
			}
			$.each($(".wv-page .wv-bar-box .wv-radio-circle.active"),function(i,item){
				_qResult.rst.push($(item).attr("data_aId"));
			})

			_evaluationResult.result.push(_qResult);
		});
		$(".wv-content").empty().append(page);
	};
	this._style5 = function(item,qIndex,_prefix){
		var _chNum = ["一","二","三","四","五","六","七","八","九","十"];
		var _childItem = item.subQuestions[_singleIndex];
		var page = $('<div class="wv-page">'+
					'	<p class="wv-title">'+_prefix+item.title+'</p>'+
					'	<p class="wv-title-sm">场景'+_chNum[_singleIndex]+":"+_childItem.title+'</p>'+
					'	<div class="wv-bar-box  wv-txt-center" data_qId="'+_childItem.id+'"></div>'+
					'</div>');
		if(_childItem.options.length == 4){
			page.find(".wv-bar-box").addClass("wv-bar-smblock");
		}else{
			page.find(".wv-bar-box").addClass("wv-bar-lgblock");
		}
		for(var i = 0; i < _childItem.options.length; i++){
			var _li = $('<div class="wv-item">'+
						'	<span class="wv-radio-lg" data_aId="'+_childItem.options[i].id+'">'+_childItem.options[i].value+
						'		<input type="radio" class="wv-radio" name="radio" />'+
						'	</span>'+
						'</div>');
			page.find(".wv-bar-box").append(_li);
		}
		page.find(".wv-radio-lg").on("click",function(){
			$(this).parents(".wv-bar-box").find(".wv-radio-lg").removeClass("active");
			$(this).addClass("active");
			_that._checkVal(page,"wv-radio-lg",1);
		});
		_singleIndex += 1;
		if(_singleIndex < item.subQuestions.length){
			_qIndex = _qIndex - 1;
		}else{
			_singleIndex = 0;
		}

		_that.btnEvent(function(){
			var _qResult = {
				id:$(".wv-page .wv-bar-box").attr("data_qId"),
				rst:[$(".wv-page .wv-bar-box .wv-radio-lg.active").attr("data_aId")]
			}
			_evaluationResult.result.push(_qResult);
		});
		//alert("style5-1");
		$(".wv-content").empty().append(page);
		//alert("style5-2");
	};

	this._checkVal = function(obj,eleClass,minChecked){
		var _count = 0;
		$.each(obj.find("."+eleClass), function(index,_item) {
			if($(_item).hasClass("active")){
				_count += 1;
			}
		});
		if(_count >= minChecked){
			$(".wv-footer .wv-btn").removeClass("wv-btn-open").addClass("wv-btn-open");
		}else{
			$(".wv-footer .wv-btn").removeClass("wv-btn-open");
		}
	}

	this.btnEvent = function(callback){
		$(".wv-footer .wv-btn").unbind("click");
		$(".wv-footer .wv-btn").bind("click",function(){
			if($(this).hasClass("wv-btn-open")){
				callback();
				_qIndex += 1;
				if(_qIndex < _lastIndex){
					wvMould.init(_qIndex);
				}else{

					_that.getResult(function(){
						$(".wv-result").show();
						$(".wv-wrap").slideUp("slow");
					
					});

				}

				$(this).removeClass("wv-btn-open");
			}
		});
	}

	this.getResult = function(callback){

		var _token = AppStorage.getValueInSession("token");
		if(openid){
			document.cookie="openid=" + openid;
		    _token = openid;
		}
		var timestap = new Date().getTime();
		var params = {
			token : _token,
			data : JSON.stringify(_evaluationResult),
			timestamp:timestap
		}
		HTTPWorker.postRequest(HTTPRouter.getResult,params,
			function(httpResult){
				if(httpResult.isSuccess()){
					AppStorage.setValueInLocalStorage("status",true);
					AppStorage.setValueInLocalStorage("result",httpResult.data);
					
					var data = JSON.parse(httpResult.data);
					wvChart.radar("chart1",data.shidaweidu);
					wvChart.barSerial("chart2",data.liuzhonghexinjingzhengli);
					wvChart.lineSerial("chart3",data.sileijijixinliyinsu);
						
					
					
					function getQueryString(name){
				      var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
				      var r = window.location.search.substr(1).match(reg);
				      if(r!=null)return  unescape(r[2]); return null;
				  }
					var name = getQueryString("source");
					var token = getQueryString("token");
					if(name === "yoparent-app" && token != null){
						HTTPWorker.postRequest(HTTPRouter.updateAssessmentState, {
								token:token
						}, function(){})
					}

					callback();

				}
			}
		);

	}


});
